package com.controller;


import com.pojo.Category;
import com.pojo.Goods;
import com.service.CategoryService;
import com.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RequestMapping("/goods")
@Controller
public class GoodController {
    @Autowired
    private GoodService goodService;
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/list")
    public String list(Model model){
        List<Goods> list=goodService.goodsList();
        model.addAttribute("GoodsList",list);
        return "goods_list";

    }
    @RequestMapping("good_show_update")
    public String good_show(Integer id,Model model){
        Goods good=goodService.findonegood(id);
        List<Category> list=categoryService.categoryList();
        model.addAttribute("categoryList",list);
        model.addAttribute("good",good);
        return "good_update";



    }
    @RequestMapping("/show_goods_update")
    public String goods_update(Goods good,MultipartFile file){
        String imgFile=null;
        if(file.isEmpty()){
            //没上传使用默认图片
            imgFile="/img/57829afaa7810f36dc21390fe4b8b53c_200782210839649_2.jpg";
        }else {
            //使用新文件名创建对象
            String newFile= UUID.randomUUID()+"-"+file.getOriginalFilename();
            File f= new File("D:/upload_image",newFile);

            if(!f.getParentFile().exists()){
                f.mkdir();

            }
            try {
                //上传文件存储到指定磁盘
                file.transferTo(f);
                //
                imgFile="/upfile/"+newFile;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        good.setPicture(imgFile);
        goodService.update_goods(good);
        return "redirect:/goods/list";

    }

    @RequestMapping("/goods_show")
    public String add_show(Model model){
        List<Category> list=categoryService.categoryList();
        model.addAttribute("categoryList",list);
        return  "goods_add";

    }
    @RequestMapping("/goods_add")
    public String goods_add(Goods goods, MultipartFile file){
       String imgFile=null;
       if(file.isEmpty()){
           //没上传使用默认图片
          imgFile="/img/57829afaa7810f36dc21390fe4b8b53c_200782210839649_2.jpg";
       }else {
           //使用新文件名创建对象
           String newFile= UUID.randomUUID()+"-"+file.getOriginalFilename();
           File f= new File("D:/upload_image",newFile);

           if(!f.getParentFile().exists()){
               f.mkdir();

           }
           try {
               //上传文件存储到指定磁盘
               file.transferTo(f);
               //
               imgFile="/upfile/"+newFile;
           } catch (IOException e) {
               e.printStackTrace();
           }

       }
        goods.setPicture(imgFile);
       goodService.add_goods(goods);
       return "redirect:/goods/list";

    }

}
